import utils.TreeNode;

/**
 * @author ZCR
 * @create 2025/9/4 23:36
 */
public class Solution98 {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isValidBST(root,null,null);
    }
    public boolean isValidBST(TreeNode root,Integer left, Integer right) {
        if (root == null) {
            return true;
        }
        if(left!=null && root.val<=left){
            return false;
        }
        if(right!=null && root.val>=right){
            return false;
        }
        return  isValidBST(root.left,left,root.val) && isValidBST(root.right,root.val,right);
    }

    public static void main(String[] args) {
        Solution98 solution = new Solution98();
        TreeNode root = TreeNode.buildTree(new int[]{5,4,6,-1,-1,3,7},-1);
        System.out.println(solution.isValidBST(root));
    }
}
